/*
 * Ext JS Library 2.2
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
Ext.onReady(function() {
    var ds = new Ext.data.Store({
        remoteSort: true,
        sortInfo: {
            field: 'last_name',
            direction: 'ASC'
        },
        proxy: new Ext.data.HttpProxy({
            url: 'participant/all'
        }),
        reader: new Ext.data.JsonReader({
            root: 'participants',
            totalProperty: 'totalCount',
            id: 'id'
        },
        [{
            name: 'first_name'
        },
        {
            name: 'last_name'
        },
        {
            name: 'institution'
        },
        {
            name: 'email'
        },
        {
            name: 'isReviewer',
            type: 'bool'
        },
        {
            name: 'stamp'            
        }])
    });
    var assignSuccess = function() {
        ds.load({
            params: {
                start: 0,
                limit: 25
            }
        });
    };
    var assignFail = function() {};
    var setAction = function() {
        var reviewers = grid.getSelectionModel().getSelections();
        var count = reviewers.length;
        if (count <= 0) {
            Ext.Msg.alert('Information', 'Please select participants to set/unset as reviewers');
            return;
        }
        var r_list = '';
        for (var i = 0; i < count; i++) {
            if (i < count - 1) {
                r_list += reviewers[i].id + ':';
            } else {
                r_list += reviewers[i].id;
            }
        }
        Ext.Ajax.request({
            url: 'participant/setreviewers',
            success: assignSuccess,
            failure: assignFail,
            params: {
                rList: r_list
            }
        });
    }
    var activateSuccess = function() {
        ds.load({
            params: {
                start: 0,
                limit: 25
            }
        });
    };
    var activateFail = function() {};
    var activateAction = function() {
        var reviewers = grid.getSelectionModel().getSelections();
        var count = reviewers.length;
        if (count <= 0) {
            Ext.Msg.alert('Information', 'Please select participants to activate');
            return;
        }
        var r_list = '';
        for (var i = 0; i < count; i++) {
            if (i < count - 1) {
                r_list += reviewers[i].id + ':';
            } else {
                r_list += reviewers[i].id;
            }
        }
        Ext.Ajax.request({
            url: 'participant/activate',
            success: activateSuccess,
            failure: activateFail,
            params: {
                rList: r_list
            }
        });
    }
    var exportAction = function() {        
        window.location = 'export/participants';        
    }
    var filters = new Ext.ux.grid.GridFilters({
        filters: [{
            type: 'string',
            dataIndex: 'first_name'
        },
        {
            type: 'string',
            dataIndex: 'last_name'
        },
        {
            type: 'string',
            dataIndex: 'institution'
        },
        {
            type: 'string',
            dataIndex: 'email'
        }]
    });
    var pagingBar = new Ext.PagingToolbar({
        pageSize: 25,
        store: ds,
        displayInfo: true,
        displayMsg: 'Displaying participants {0} - {1} of {2}',
        emptyMsg: "No participants to display",
        items: [{
            text: 'Set/Unset reviewer',
            handler: setAction,
            waitMsg: 'Setting reviewers...',
            tooltip: 'Select participants and click to set/unset as reviewers'
        },
        '|',
        {
            text: 'Activate',
            handler: activateAction,
            waitMsg: 'Activating participants...',
            tooltip: 'Select participants and click to activate their acounts'
        },'|',
        {
            text: 'Export',
            handler: exportAction,
            waitMsg: 'Exporting participants...',
            tooltip: 'Click to export participants to xls'
        }]
    });
    function renderEmail(value, p, r) {
        return String.format('<a href="mailto:{0}">{0}</a>', value);
    }
    function renderReviewer(value, p, r) {
        var yes = '<img src="../images/2.jpg">';
        var no = '<img src="../images/1.jpg">';
        if (value) {
            return yes;
        } else {
            return no;
        }
    }
    function renderActive(value, p, r) {
        var yes = '<img src="../images/2.jpg">';
        var no = '<img src="../images/1.jpg">';
        if (value == 1) {
            return yes;
        } else {
            return no;
        }
    } 
    // create the grid
    var grid = new Ext.grid.GridPanel({
        store: ds,
        columns: [{
            header: "Last Name",
            width: 150,
            dataIndex: 'last_name',
            sortable: true
        },
        {
            header: "First Name",
            width: 150,
            dataIndex: 'first_name',
            sortable: true
        },
        {
            header: "Institution",
            width: 200,
            dataIndex: 'institution',
            sortable: true
        },
        {
            header: "Email",
            width: 200,
            dataIndex: 'email',
            renderer: renderEmail,
            sortable: true
        },
        {
            header: "Reviewer",
            width: 80,
            dataIndex: 'isReviewer',
            sortable: true,
            renderer: renderReviewer
        },
        {
            header: "Activated",
            width: 80,
            dataIndex: 'stamp',
            sortable: true,
            renderer: renderActive
        }],
        plugins: filters,
        renderTo: 'data-grid',
        width: 860,
        height: 400,
        title: 'Participants',
        frame: true,
        loadMask: true,
        bbar: pagingBar,
        viewConfig: {
            forceFit: true
        }
    });
    ds.load({
        params: {
            start: 0,
            limit: 25
        }
    });
});